const app = getApp()
const util = require('../../utils/util.js')
Page({
  data: {
    host:app.globalData.host,
    login:'',
    modalName:'',
    topic_id:'',
    modalName:'',
    nofollow_parent_id:'',
  },

  showModal:function(e){
    this.setData({
      modalName: 'modal1',
      nofollow_parent_id:e.target.dataset.parent_id
    })
  },
  hideModal:function(){
    this.setData({
      modalName: '',
    })
  },
  ViewImage(e) {
    var array = this.data.pageList[e.currentTarget.dataset.index].image_array;
    for(var i = 0;i<array.length;i++){
      array[i] = this.data.host+array[i];
    }
    wx.previewImage({
      urls: array,
      current: e.currentTarget.dataset.url
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad:function(e){
    // //内容列表
    // this.getMsgList(1,'');
    // this.setData({
    //   login:app.globalData.login
    // })
    // console.log('app.globalData.login')
    // console.log(app.globalData.login)
  },

  changeFollow:function(parent_id,child_id,status){
    var pageList = this.data.pageList;
    if(status == 1){
      for(var i =0 ;i< pageList.length;i++){
        if(pageList[i].parent_id == parent_id && pageList[i].child_id == child_id ){
          pageList[i].follow = status;
        }
      }
    }
    if(status == 0){
      for(var i =0 ;i< pageList.length;i++){
        if(pageList[i].parent_id == parent_id ){
          pageList[i].follow = status;
        }
      }
    }
    this.setData({
      pageList:pageList
    });
  },
  /**
   * 关注
   * @param {*} e
   */
  follow:function(e){
    var data = e.target.dataset, parent_id =data.parent_id,child_id = data.child_id,index = data.index;
    var that = this;
    var row = 'pageList['+index+'].follow';
    util.request('follow/add', {type:1,parent_id:parent_id,child_id:child_id}, function (data) {
      console.log(data);
      if(data.code == 1){
       that.changeFollow(parent_id,child_id,1);
        wx.showToast({
          title: '关注成功',
          icon: 'success',
          duration: 1500
        })
      }else{
        util.fail(data.msg,that);
      }
    });
  },

  /**
   * 取消关注
   * @param {*} e
   */
  nofollow:function(e){
    var parent_id = this.data.nofollow_parent_id;
    var that = this;
    util.request('follow/delete', {type:1,parent_id:parent_id}, function (data) {
      console.log(data);
      if(data.code == 1){
        that.setData({
          nofollow_parent_id:''
        })
        that.changeFollow(parent_id,'',0);
        that.hideModal();
        wx.showToast({
          title: '操作成功',
          icon: 'success',
          duration: 1500
        })
      }else{
        that.setData({
          nofollow_parent_id:''
        })
        util.fail(data.msg,that);
      }
    });
  },

  /**
   * 收藏/取消收藏
   * @param {*} e
   */
  collect:function(e){
    var index = e.currentTarget.dataset.index,data = this.data.pageList[index],that = this;
    var collect = data.collect;
    util.request('collect/save', {related_id:data['id']}, function (data) {
      if(data.code == 1){
        var row = 'pageList['+index+'].collect';
        if(collect.is_collect > 0){
          that.setData({
              [row]:{
                count:collect.count-1,
                is_collect:0,
              }
          })
        }else{
          that.setData({
            [row]:{
              count:collect.count+1,
              is_collect:1,
            }
        })
        }
      }else{
        util.fail(data.msg,that);
      }
    });
  },



  /**
   * 点赞/取消点赞
   * @param {*} e
   */
  thumb:function(e){
    var index = e.currentTarget.dataset.index,data = this.data.pageList[index],that = this;
    var thumb = data.thumb;
    util.request('thumb/save', {type:3,related_id:data['id']}, function (data) {
      if(data.code == 1){
        var row = 'pageList['+index+'].thumb';
        if(thumb.is_thumb > 0){
          that.setData({
              [row]:{
                count:thumb.count-1,
                is_thumb:0,
              }
          })
        }else{
          that.setData({
            [row]:{
              count:thumb.count+1,
              is_thumb:1,
            }
        })
        }
      }else{
        util.fail(data.msg,that);
      }
    });
  },

  onShow:function(){
    //内容列表
    this.getMsgList(1,'');
    this.setData({
      login:app.globalData.login
    })
    console.log('app.globalData.login')
    console.log(app.globalData.login)
  },

  onPullDownRefresh: function () {
    if (!this.loading) {
      wx.showNavigationBarLoading();
      this.getMsgList(1,'top');
    }
  },

  onReachBottom: function () {
    var that = this;
    if (!this.data.loading && this.data.page < this.data.pages) {
      that.setData({
        isHideLoadMore: false,
        page: this.data.page+1
      });
      this.getMsgList(that.data.page, 'bottom');
    }
  },

  getMsgList: function(pageNo,pos) {
    var url = 'article/index',param = {type:3};
    param.pageNo = pageNo;
    console.log(param);
    util.pageList(this,url,param, pos);
  },
})
